package com.xyy.dfapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.xyy.dfapi.config.ComentCfg;
import com.xyy.dfapi.pojo.*;
import com.xyy.dfapi.service.ErpService;
import com.xyy.dfapi.utils.CacheUtils;
import com.xyy.dfapi.utils.CfgUtils;
import com.xyy.dfapi.utils.Constant;
import com.xyy.dfapi.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ErpServiceImpl implements ErpService {
    @Autowired
    private HttpUtils httpUtils;
    static ComentCfg comentCfg;

    @PostConstruct
    private void init() {
        comentCfg = CfgUtils.getAppDefaultCfg();
        CacheUtils.set(Constant.CONFIG, JSON.toJSONString(comentCfg));
    }

    @Override
    public ErpResultDto<List<ErpGoodsDto>> erpQueryGoods() {
        String goodsUrl = comentCfg.getErpApiHost() + comentCfg.getErpQueryGoods();
        return httpUtils.dfPost(goodsUrl, "{}");
    }

    @Override
    public ErpResultDto<List<ErpGoodsStockDto>> erpQueryGoodsStock() {
        String goodsStockUrl = comentCfg.getErpApiHost() + comentCfg.getErpQueryGoodsStock();
        return httpUtils.dfPost(goodsStockUrl, "{}");
    }

    @Override
    public ErpResultDto<List<ErpCustDto>> erpQueryCus() {
        String queryCustUrl = comentCfg.getErpApiHost() + comentCfg.getErpQueryCus();
        return httpUtils.dfPost(queryCustUrl, "{}");
    }

    /**
     * 新增销售订单信息
     */
    @Override
    public ErpResultDto insertOrder(ErpInsertOrderDto orderDto) {
        log.info("【ERP导入订单数据】: {}", JSON.toJSONString(orderDto));
        String insertOrderUrl = comentCfg.getErpApiHost() + comentCfg.getErpInsertOrder();
        return httpUtils.dfPost(insertOrderUrl, JSON.toJSONString(orderDto));
    }

    /**
     * 新增客户首营资料
     *
     * @param customerDto
     */
    @Override
    public ErpResultDto insertCustomer(ErpInsertCustomerDto customerDto) {
        log.info("【ERP导入客户数据】: {}", JSON.toJSONString(customerDto));
        String insertCustomerUrl = comentCfg.getErpApiHost() + comentCfg.getErpInsertCus();
        return httpUtils.dfPost(insertCustomerUrl, JSON.toJSONString(customerDto));

    }

    /**
     * 查询订单状态
     *
     * @param orderNo
     */
    @Override
    public ErpResultDto queryOrderState(String orderNo) {
        Map<String, String> order = new HashMap<>();
        order.put("corOrderCode", orderNo);
        String queryOrder = comentCfg.getErpApiHost() + comentCfg.getErpQueryOrder();
        return httpUtils.dfPost(queryOrder, JSON.toJSONString(order));
    }

    /**
     * 查询电子发票
     *
     * @param orderNo
     */
    @Override
    public ErpResultDto queryOrderInvoice(String orderNo) {
        Map<String, String> order = new HashMap<>();
        order.put("corOrderCode", orderNo);
        String queryOrderInvoice = comentCfg.getErpApiHost() + comentCfg.getErpQueryInvoice();
        return httpUtils.dfPost(queryOrderInvoice, JSON.toJSONString(order));
    }

    /**
     * 分页查询客户信息
     *
     * @param page 当前页
     * @param rows 每页条数
     */
    @Override
    public ErpResultDto queryCusPage(int page, int rows) {
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("rows", rows);
        String queryCusPage = comentCfg.getErpApiHost() + comentCfg.getQueryCusPage();
        return httpUtils.dfPost(queryCusPage, JSON.toJSONString(map));
    }

}
